package com.zfei.gfb.util;

import com.zfei.gfb.core.Conf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PythonUtil {

    private static final Logger logger = LoggerFactory.getLogger(PythonUtil.class);
    private static final String pythonHome = Conf.getString("python_home", "/opt/python37/bin/python3.7");
    private static final String profile = Conf.getString("spring.profiles.active", "test");

    public static void genWallMp3s(int id, String... persons) {
        for (String person : persons) {
            genWallMp3(id, person);
        }
    }

    public static void genWallMp3(int id, String person) {

        logger.info("profile={}", profile);
        String path = Conf.getString("py_wall_path", "/root/voice/onebutton/kdxf_" + person + "_wall_" + profile + ".py");
        genCommonMp3(id, path);
    }

    public static void genLongWallMp3(int id, String person) {

        logger.info("profile={}", profile);
        String path = Conf.getString("py_long_wall_path", "/root/voice/onebutton/long_kdxf_wall_" + profile + ".py");
        genLongCommonMp3(id, person, path);
    }


    public static void genArticleMp3s(int id, String... persons) {
        for (String person : persons) {
            genArticleMp3(id, person);
        }
    }

    public static void genArticleMp3(int id, String person) {
        String path = Conf.getString("py_article_path", "/root/voice/onebutton/kdxf_" + person + "_article_" + profile + ".py");
        genCommonMp3(id, path);
    }

    public static void genArticleBriefMp3(int id) {

        logger.info("profile={}", profile);
        String path = Conf.getString("py_article_brief_path", "/root/voice/onebutton/kdxf_onebutton_brief_" + profile + ".py");
        genCommonMp3(id, path);
    }

    public static void genBriefMp3s(int id, String... persons) {
        for (String person : persons) {
            genBriefMp3(id, person);
        }
    }

    public static void genBriefMp3(int id, String person) {
        logger.info("-----profile={}", profile);
        String path = Conf.getString("py_brief_path", "/root/voice/onebutton/kdxf_" + person + "_brief_" + profile + ".py");
        logger.info("-----path={}", path);
        genCommonMp3(id, path);
    }

    public static void genCommonMp3(int id, String tablePyPath) {

        logger.info("start gen tablePyPath mp3,prkey={}", id);

        try {
            String prkey = String.valueOf(id);
            String[] args = new String[]{pythonHome, tablePyPath, prkey};
            Process pr = Runtime.getRuntime().exec(args);
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String line;

            while ((line = in.readLine()) != null) {
                logger.info(line);
            }
            in.close();
            pr.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        logger.info("end ...");
    }

    public static void genLongCommonMp3(int id, String person, String tablePyPath) {

        logger.info("start gen tablePyPath mp3,prkey={}", id);

        try {
            String prkey = String.valueOf(id);
            String[] args = new String[]{pythonHome, tablePyPath, person,prkey};
            Process pr = Runtime.getRuntime().exec(args);
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String line;

            while ((line = in.readLine()) != null) {
                logger.info(line);
            }
            in.close();
            pr.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        logger.info("end ...");
    }

}
